#!/bin/sh
FILE=$1
ACTION=$2
export REMOTE_USER=importtool
EDIR=/var/sipman3/main/maint/var/sipman/named-database
readFile ()
{
  FILE=$1
  if [ -f $FILE ]; then
    cat $FILE
  else
    printf "-"
  fi
}

splitName ()
{
  WDNSNAME=$1
  HNAME=`echo $WDNSNAME | awk -F. '{ print $1 }'`
  DNAME="${WDNSNAME:`expr ${#HNAME} + 1`}"
  printf "$HNAME $DNAME"
}

. /etc/sipman.conf
if [ "$FILE" = "" ]; then
  echo "$0 [importfilefile] (clean)"
  exit
elif [ ! -f $FILE ]; then
  echo "No import file specified."
  exit
fi
if [ "$ACTION" = "clean" ]; then
  rm -vfR /var/sipman3/data/local/ip-database/*
  rm -vfR /var/sipman3/data/local/dns-database/*
  rm -vfR /var/sipman3/data/local/mac-database/*
  rm -vfR /var/sipman3/data/local/log/*
fi
REMOTE_USER=importtool
sort -k 2 $FILE | while read DATE IPNUM RECTYPE MAC IPADDR HOST DOMAIN DHCPNAME ADDRTYPE COMMENT
do
  NETDIR=$SIM_IPDATAB/`echo $IPADDR | sed 's#\.#/#g'`
  if [ "$ACTION" = "exist" ]; then
    if [ -d "$NETDIR" ]; then
      POLL=oke
    else
      POLL=skip
    fi
  else
    POLL=oke
  fi
  if [ "$POLL" = "oke" ]; then
    if [ -d $NETDIR ]; then
      echo "Node $IPADDR already exists."
    else
      $SIM_UPDATET addnode $IPADDR
      $SIM_UPDATET changeStatus $IPADDR Confirmed
    fi
    if [ "$RECTYPE" != "`readFile $NETDIR/sipmanrectype.txt`" ]; then
      $SIM_UPDATET modSipmanRectype $IPADDR $RECTYPE
    fi

    #dhcp name
    ORGDHCPNAME=`readFile $NETDIR/dhcpname.txt`
    if [ "$DHCPNAME" != "" ] && [ "$RECTYPE" = "dyn" ]; then
      case $DHCPNAME in
        (NoHostName|viaeth0)  SKIP=yes ;;
        (*)
          if [ "$ORGDHCPNAME" != "$DHCPNAME" ]; then
            echo "$DHCPNAME" > $NETDIR/dhcpname.txt
            echo "DHCP name updated to $DHCPNAME ($ORGDHCPNAME)."
          fi
        ;;
      esac
    fi
    CURRMANTYPE=`readFile $NETDIR/managementtype.txt`
    if [ "$CURRMANTYPE" != "poll" ] && [ "$CURRMANTYPE" != "snmp" ]; then
      $SIM_UPDATET modManType $IPADDR snmp
    fi
    case $ADDRTYPE in
       (dhcpdyn)   ADDRTYPE=wks ;;
       (dhcpfix)   ADDRTYPE=fix ;;
       (other)     ADDRTYPE=srv ;;
       (unknown)   ADDRTYPE=unk ;;
       (undefined) ADDRTYPE=unk ;;
    esac
    CURRADDRTYPE=`readFile $NETDIR/nodetype.txt`
    if [ "$CURRADDRTYPE" != "$ADDRTYPE" ]; then
      $SIM_UPDATET modNodeType $IPADDR $ADDRTYPE
    fi

    if [ "$RECTYPE" = "ext" ]; then
      echo "Skipping dnsstuff for $IPADDR, is ext."
    elif [ "$RECTYPE" = "auto" ]; then
      echo "The dns stuff for auto types $IPADDR should be done automatically."
    elif [ "$HOST" != "-" ] && [ "$DOMAIN" != "-" ]; then
      DNSNAME=$HOST.$DOMAIN
      if [ "manual `splitName $DNSNAME`" != "`readFile $NETDIR/dnsname.txt`" ]; then
        $SIM_UPDATET modDnsName $IPADDR `splitName $DNSNAME`
      fi
      echo "$SIM_UPDATET modDnsName $IPADDR `splitName $DNSNAME`"
      if [ -f "$EDIR/$DNSNAME" ]; then
        echo "  FOUND $EDIR/$DNSNAME"
        cat $EDIR/$DNSNAME | while read DNSTYPE ADDNAME
        do
          DNSNAME="`splitName $ADDNAME`"
          $SIM_UPDATET addDnsReccord $IPADDR "$DNSTYPE `splitName $ADDNAME`" 
        done
      else
        echo "$EDIR/$DNSNAME not found."
      fi
    fi
    CURRCOMMENT="`readFile $NETDIR/comment.txt`"
    if [ "$CURRCOMMENT" != "$COMMENT" ] && [ "$COMMENT" != "." ]; then
      $SIM_UPDATET modComment $IPADDR "$COMMENT"
    fi
    case $RECTYPE in
      (ext|dyn|res)
        CURRMAC=`readFile $NETDIR/dhcpmacaddr.txt`
        if [ "$MAC" != "fixed" ] && [ "$CURRMAC" != "$MAC" ]; then
          $SIM_UPDATET modDhcpmac $IPADDR $MAC
        fi
      ;;
    esac
    #leep 1
  fi
done

if [ "$ACTION" = "clean" ]; then
  /var/sipman3/main/bin/sipman-starter.sh local run-all verbose
fi
